<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>机构编辑</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- 可选的Bootstrap主题文件（一般不用引入） -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="/css/font-awesome.css">
    <link rel="stylesheet" href="/css/base.css">
    <link rel="stylesheet" href="/css/djs.css">

    <script src="/json/CityJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/json/ProJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/json/DistrictJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/user.js"></script>
    <script type="text/javascript" src="/js/base.js"></script>
</head>
<body>
<div id="top">

</div>
<div class="container">
    <div id="asideAffix" class="aside" data-spy="affix">
    </div>
    <div class="content centers editcontent" style="margin-top: 0;" >
        <form role="form" id="editForm" class="form-horizontal">
            <div class="form-group">
                <div class="col-sm-12">
                    <label for="onePass" class="col-sm-2">组织一号通：</label><input type="text" id="onePass" required
                                                                               class="col-sm-4">
                </div>

                <div class="col-sm-12">
                    <label for="orgName" class="col-sm-2">组织名称：</label><input type="text" id="orgName" required
                                                                              class="col-sm-4">
                    <input type="hidden" id="orgId"><input type="hidden" id="parentOrgId">
                </div>

                <div class="col-sm-12">
                    <label for="selDistrict" class="col-sm-2">地址信息：</label>
                    <div class="col-sm-6" style="padding-left: 0;">
                        <select id="selProvince" required>
                            <option value="">--请选择省份--</option>
                        </select>
                        <select id="selCity" required>
                            <option value="">--请选择城市--</option>
                        </select>
                        <select id="selDistrict" required>
                            <option value="">--请选择区/县--</option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-12">
                    <label for="address" class="col-sm-2">详细地址：</label><input type="text" id="address" required
                                                                              class="col-sm-4">
                </div>
                <div class="col-sm-12">
                    <label for="zipCode" class="col-sm-2">邮政编码：</label><input type="text" id="zipCode" required
                                                                              class="col-sm-4">
                </div>
                <div class="col-sm-12">
                    <label for="principal" class="col-sm-2">负责人姓名：</label><input type="text" id="principal" required
                                                                                 class="col-sm-4">
                </div>
                <div class="col-sm-12">
                    <label for="phone" class="col-sm-2">负责人电话：</label><input type="text" id="phone" required
                                                                             class="col-sm-4">
                </div>
            </div>
            <div>
                <button type="button" class="col-sm-2 btn btn-primary" id="rollback"
                        style="border-color: #ffd632;background: #ffd632 linear-gradient(to bottom,#ffd632 0,#ffd632 100%);">
                    返回
                </button>
                <button class="col-sm-2 btn btn-primary" id="create"
                        style="border-color: #ffd632;background: #ffd632 linear-gradient(to bottom,#ffd632 0,#ffd632 100%);">
                    保存
                </button>
            </div>
        </form>

    </div>
</div>
</body>

<script type="text/javascript">
    $(function () {

        for(var i = 0;i<location.search.substr(1).split("&").length;i++){
            if("orgId" == location.search.substr(1).split("&")[i].split("=")[0]){
                if (location.search.substr(1).split("&")[i].split("=")[1] != undefined && location.search.substr(1).split("&")[i].split("=")[1] != 0 ) {
                    getData(location.search.substr(1).split("&")[i].split("=")[1]);
                }
            }else if("parentOrgId" == location.search.substr(1).split("&")[i].split("=")[0]){
                if (location.search.substr(1).split("&")[i].split("=")[1] != undefined && location.search.substr(1).split("&")[i].split("=")[1] != 0 ) {
                    $("#parentOrgId").val(location.search.substr(1).split("&")[i].split("=")[1]);
                }
            }
        }

        $.each(province, function (k, p) {
            var option = "<option value='" + p.ProID + "'>" + p.ProName + "</option>";
            $("#selProvince").append(option);
        });
    });

    $("#selProvince").change(function () {
        provinceChange($("#selProvince").val())
    });
    $("#selCity").change(function () {
        cityChange($("#selCity").val())
    });

    function cityChange(selValue) {
        $("#selDistrict").find("option:gt(0)").remove();
        $.each(District, function (k, p) {
            if (p.CityID == selValue) {
                var option = "<option value='" + p.Id + "'>" + p.DisName + "</option>";
                $("#selDistrict").append(option);
            }
        });
    }

    function provinceChange(selValue) {
        $("#selDistrict").find("option:gt(0)").remove();
        $("#selCity").find("option:gt(0)").remove();
        $.each(city, function (k, p) {
            if (p.ProID == selValue) {
                var option = "<option value='" + p.CityID + "'>" + p.CityName + "</option>";
                $("#selCity").append(option);
            }
        });
    }

    function getData(orgId) {
        $.ajax({
            url: '/org/info',// 跳转到 action
            headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
            data: JSON.stringify({
                "orgId": orgId
            }),
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == 0) {
                    if (undefined != data.data) {

                        $("#onePass").val(data.data.onePass);
                        $("#orgName").val(data.data.orgName);
                        $("#orgId").val(data.data.orgId);
                        $("#parentOrgId").val(data.data.orgParent);
                        $("#selProvince").val(data.data.province);
                        provinceChange(data.data.province);
                        $("#selCity").val(data.data.city);
                        cityChange(data.data.city);
                        $("#selDistrict").val(data.data.county);
                        $("#address").val(data.data.address);
                        $("#zipCode").val(data.data.zipCode);
                        $("#principal").val(data.data.principal);
                        $("#phone").val(data.data.phone);
                    }
                } else {
                    alert(data.statusMessage);
                }
            },
            error: function () {
                // view("异常！");
                alert("用户未登录或者服务器连接异常！");
            }
        });
    }

    $("#editForm").submit(
            function () {
                if(!areYouSure("")){
                    return false;
                }
                saveOrg();
                return false;
            }
    );

    function saveOrg() {
        $.ajax({
            url: '/org/save',// 跳转到 action
            headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
            data: JSON.stringify({
                "orgParent": $("#parentOrgId").val(),
                "orgId": $("#orgId").val(),
                "onePass": $("#onePass").val(),
                "orgName": $("#orgName").val(),
                "province": $("#selProvince").val(),
                "city": $("#selCity").val(),
                "county": $("#selDistrict").val(),
                "address": $("#address").val(),
                "zipCode": $("#zipCode").val(),
                "principal": $("#principal").val(),
                "phone": $("#phone").val()
            }),
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == 0) {
                    back();
                } else {
                    alert(data.statusMessage);
                }
            },
            error: function () {
                // view("异常！");
                alert("服务器感冒了1！");
            }
        });
    }

    $("#rollback").click(function () {
        back()
    });

    function back() {
        location.href = "/html/org/orgList.html";
    }
</script>
</html>